package com.yy.job.facade.validator;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yy.job.common.enums.ErrorCodeEnum;
import com.yy.job.core.config.JobConfigXmlContext;
import com.yy.job.core.config.xml.JobStepXmlConfig;
import com.yy.job.core.config.xml.JobXmlConfig;
import com.yy.job.facade.model.YYJobModel;
import com.yy.job.facade.model.YYJobStepModel;
import com.yy.job.utils.exception.YYJobRuntimeExceptionUtils;

@Service
public class YYJobStepModelValidator {

	@Resource
	private JobConfigXmlContext jobConfigParser;
	
	public void validate(YYJobModel yyJobModel, List<YYJobStepModel> yyJobStepModels) {
		if(CollectionUtils.isEmpty(yyJobStepModels)) {
			return;
		}
		
		yyJobStepModels.forEach(e->{
			this.validate(yyJobModel, e);
		});
	}

	private void validate(YYJobModel yyJobModel, YYJobStepModel yyJobStepModel) {
		if(yyJobStepModel == null) {
			YYJobRuntimeExceptionUtils.throwException(ErrorCodeEnum.CMN_PARAMETER_ERROR, "step is null");
		}
		
		// 获取xml配置信息
		JobXmlConfig jobConfig = this.jobConfigParser.getProjectXmlConfig().findJobConfig(yyJobModel.getModuleCode(), yyJobModel.getJobCode());
		if (StringUtils.isBlank(yyJobStepModel.getStepCode())) {
			YYJobRuntimeExceptionUtils.throwException(ErrorCodeEnum.CMN_PARAMETER_ERROR, "stepCode is null");
		}
		JobStepXmlConfig jobStepConfig = jobConfig.findJobStepConfig(yyJobStepModel.getStepCode());
		if (jobStepConfig == null) {
			YYJobRuntimeExceptionUtils.throwException(ErrorCodeEnum.SUBMIT_STEP_ERROR, yyJobModel.getModuleCode(),
					yyJobModel.getJobCode(), yyJobStepModel.getStepCode(), "no step config");
		}

	}

}
